﻿using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Text;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YArchitech.LIB;
using YRevitJig;

namespace YJKRoomBehavior
{
	internal class RectJig : XUiJigBase
	{
		public static SolidBrush SizeTextBrush { get; set; }

		public System.Drawing.Color SizeLineColor { get; set; }

		public int SizeOffset { get; set; }

		public int BorderLength { get; set; }

		public Font SizeFont { get; set; }

		public StringFormat TextFormat { get; set; }

		public RectJig(UIDocument uidoc) : base(uidoc)
		{
			this.m_uidoc = uidoc;
			this.SizeLineColor = System.Drawing.Color.FromArgb(126, 163, 255);
			int height = Screen.PrimaryScreen.Bounds.Height;
			this.SizeOffset = (int)(0.006 * (double)height);
			this.BorderLength = (int)(0.04 * (double)height);
			this.SizeFont = new Font("Arial", 2.5f * (float)this.SizeOffset, GraphicsUnit.Pixel);
			RectJig.SizeTextBrush = new SolidBrush(base.JigPen.Color);
			this.TextFormat = new StringFormat(StringFormatFlags.NoClip);
			this.TextFormat.Alignment = StringAlignment.Center;
			this.TextFormat.LineAlignment = StringAlignment.Center;
		}

		public void DrawJigCurveRect(string strPromptStart, string strPromptEnd)
		{
			XYZ zero = XYZ.Zero;
			this.m_rectjig = new XRectJig(this.m_uiDoc);
			try
			{
				this.StartPoint = this.m_rectjig.PickStartPoint(strPromptStart);
				if (this.StartPoint == null)
				{
					this.m_rectjig.Dispose();
				}
				if (this.m_rectjig.PickEndPoint(this.StartPoint, this.m_uiDoc.ActiveView.RightDirection, strPromptEnd) == null)
				{
					this.m_rectjig.Dispose();
				}
				this.m_rectjig.Dispose();
			}
			catch (Exception)
			{
				this.m_rectjig.Dispose();
			}
		}

		public EStage JigStage { get; set; }

		public XYZ RectAngle { get; set; }

		public bool DrawJigCurveRect(XYZ vtAngle, string str1 = "请选择表格插入点：", string str2 = "请确定表格生成范围：")
		{
			try
			{
				this.StartPoint = base.UiDoc.Selection.PickPoint(str1);
				this.RectAngle = vtAngle;
				this.EndPoint = base.UiDoc.Selection.PickPoint(str2);
			}
			catch
			{
				this.StartPoint = null;
				this.Dispose();
				return false;
			}
			return true;
		}

		public override void Draw(PaintEventArgs paintEventArgs)
		{
			base.Draw(paintEventArgs);
			try
			{
				XYZ xyz = base.Client2Revit(this.m_ptCur);
				XYZ xyzpoint = Line.CreateUnbound(this.StartPoint, this.RectAngle).Project(xyz).XYZPoint;
				XYZ xyz2 = xyz + (this.StartPoint - xyzpoint);
				System.Drawing.Point point = base.Revit2Client(this.StartPoint);
				System.Drawing.Point point2 = base.Revit2Client(xyzpoint);
				System.Drawing.Point point3 = base.Revit2Client(xyz2);
				XGraphics xgraphics = new XGraphics(paintEventArgs.Graphics, base.JigPen);
				new XLine2D(point, point2).Draw(xgraphics);
				new XLine2D(point2, this.m_ptCur).Draw(xgraphics);
				new XLine2D(this.m_ptCur, point3).Draw(xgraphics);
				new XLine2D(point3, point).Draw(xgraphics);
				this.m_dWidth = this.StartPoint.DistanceTo(xyz2);
				this.DrawLineSize(xyzpoint, xyz, xgraphics);
			}
			catch (Exception)
			{
			}
		}

		public void DrawLineSize(XYZ ptFrom, XYZ ptTo, XGraphics graphics)
		{
			System.Drawing.Point point = base.Revit2Client(ptFrom);
			System.Drawing.Point point2 = base.Revit2Client(ptTo);
			XUiJigBase.SortTwoPoint(ref point, ref point2);
			if (XUiJigBase.TwoPointDist(point, point2) < 10)
			{
				return;
			}
			double num = XUiJigBase.TwoPointAngle(point, point2);
			double dAngle = num + 4.71238898038469;
			System.Drawing.Color color = graphics.m_pen.Color;
			graphics.m_pen.Color = this.SizeLineColor;
			System.Drawing.Point point3 = XUiJigBase.OffsetPoint(point, dAngle, this.SizeOffset);
			System.Drawing.Point point4 = XUiJigBase.OffsetPoint(point2, dAngle, this.SizeOffset);
			System.Drawing.Point ptEnd = XUiJigBase.OffsetPoint(point3, dAngle, this.BorderLength);
			new XLine2D(point3, ptEnd).Draw(graphics);
			System.Drawing.Point ptEnd2 = XUiJigBase.OffsetPoint(point4, dAngle, this.BorderLength);
			new XLine2D(point4, ptEnd2).Draw(graphics);
			System.Drawing.Point ptFrom2 = XUiJigBase.OffsetPoint(point3, dAngle, this.BorderLength - this.SizeOffset);
			point4 = XUiJigBase.OffsetPoint(point4, dAngle, this.BorderLength - this.SizeOffset);
			new XLine2D(XUiJigBase.OffsetPoint(ptFrom2, num, -this.SizeOffset), XUiJigBase.OffsetPoint(point4, num, this.SizeOffset)).Draw(graphics);
			double feet = ptFrom.DistanceTo(ptTo);
			System.Drawing.Point point5 = XUiJigBase.OffsetPoint(XUiJigBase.OffsetPoint(ptFrom2, num, XUiJigBase.TwoPointDist(point, point2) / 2), dAngle, (int)((double)this.SizeOffset + (double)this.SizeFont.Height / 2.0));
			PointF point6 = new PointF((float)point5.X, (float)point5.Y);
			float angle = -(float)(num / Math.PI * 180.0);
			graphics.m_graphics.TextRenderingHint = TextRenderingHint.SingleBitPerPixel;
			Matrix transform = graphics.m_graphics.Transform;
			Matrix transform2 = graphics.m_graphics.Transform;
			transform2.RotateAt(angle, point6);
			string s = string.Format("{0:F1}", NumbericTools.feetTomm(feet));
			graphics.m_graphics.Transform = transform2;
			graphics.m_graphics.DrawString(s, this.SizeFont, RectJig.SizeTextBrush, point6, this.TextFormat);
			graphics.m_graphics.Transform = transform;
			graphics.m_pen.Color = color;
		}

		public new void Dispose()
		{
			base.Dispose();
			this.SizeFont.Dispose();
			this.TextFormat.Dispose();
			RectJig.SizeTextBrush.Dispose();
		}

		private UIDocument m_uidoc;

		public XYZ StartPoint;

		public XYZ EndPoint;

		private XRectJig m_rectjig;

		public double m_dWidth;
	}
}
